package com.scrumtrek.trainings.tdd;

import com.amazon.AmazonItem;
import com.amazon.AmazonStoreWebService;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: neee
 * Date: 11.09.13
 * Time: 12:51
 * To change this template use File | Settings | File Templates.
 */
public class Store implements IStore<Book> {
    public void setIPaySystem(IPaySystem iPaySystem) {
        this.iPaySystem = iPaySystem;
    }

    IPaySystem iPaySystem;
    private List<Book> items = new ArrayList<Book>();

    public void setAmazonStoreWebService(AmazonStoreWebService amazonStoreWebService) {
        this.amazonStoreWebService = amazonStoreWebService;
    }

    private AmazonStoreWebService amazonStoreWebService;

    public void addItem(Book item) {
        items.add(item);
    }

    public void removeItem(Book item) {
        items.remove(item);
    }

    public List<Book> getItems() {
        return items;
    }

    public List<AmazonItem> getAmazonItems (String seesionId){
        if (amazonStoreWebService != null && seesionId != null){
            return amazonStoreWebService.getItems(seesionId);
        }
        return null;
    }

    public String getSessionAmazon() {
        if (amazonStoreWebService != null){
            return amazonStoreWebService.getSessionId();
        }
        return "";
    }

    public boolean sell(String sessionId, long itemId, int quantity) {
        if (amazonStoreWebService != null){
            return amazonStoreWebService.sell(sessionId, itemId, quantity);
        }
        return false;
    }

    public void sell(Book item) {
        iPaySystem.increaseProfit(item.getPrice());
        removeItem(item);
    }
}
